package com.example.onlinetaxi.ui.activity.StartToEndPage;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.R;
import com.example.onlinetaxi.dto.DataResponse;
import com.example.onlinetaxi.dto.OrderDto.CompletedOrderDto;
import com.example.onlinetaxi.ui.activity.CommentPage.CommentActivity;
import com.example.onlinetaxi.util.OKHttpUtil.GetDataResponse;
import com.example.onlinetaxi.util.OKHttpUtil.HttpUrl;
import com.example.onlinetaxi.util.OKHttpUtil.OKHttpUtil;
import com.example.onlinetaxi.util.TimeUtil;
import com.example.onlinetaxi.view.Dialog.MyDialog;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

public class DriverArrivedFragment extends Fragment {

    private View rootView;
    private Button driverArrivedBtn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_driver_arrived, container, false);

        initView();

        initEvent();

        return rootView;
    }

    private void initEvent() {
        driverArrivedBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 司机到达事件
                showDialog();
            }
        });
    }

    private void showDialog() {
        View.OnClickListener onCancelClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 取消，不操作
            }
        };

        View.OnClickListener onConfirmClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击确认
                // 发送请求，创建已完成订单
                Map<String, String> map = new HashMap<>();

                map.put("orderId", LocalStorage.currentOrder.getReceivedOrderId().toString());

                // 发送订单完成请求
                String response = OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "receivedOrder", "completeOrder");
                DataResponse dataResponse = GetDataResponse.getDataResponse(response);

                if (dataResponse.getCode().equals("200")) {
                    // 成功
                    Toast.makeText(getActivity(), "订单结束！请提醒用户支付订单~", Toast.LENGTH_SHORT).show();

                    // 获取订单
                    map.clear();
                    map.put("orderId", LocalStorage.currentOrder.getReceivedOrderId().toString());

                    String responseStr = OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "completedOrder", "getCompletedOrder");

                    DataResponse dataResponse2 = GetDataResponse.getDataResponse(responseStr);

                    // 配置合适订单
                    Gson gson = new Gson();
                    LocalStorage.currentOrder = gson.fromJson(dataResponse2.getData().get("order").toString(), CompletedOrderDto.class);

                    // 转换时间格式
                    LocalStorage.currentOrder = TimeUtil.setRightTimeFormat(LocalStorage.currentOrder);

                    LocalStorage.chosenOrder = LocalStorage.currentOrder;

                    // 清空当前订单
                    LocalStorage.currentOrder = null;
                    LocalStorage.currentOrderId = null;

                    // 跳转到评价乘客页面
                    startActivity(new Intent(getActivity(), CommentActivity.class));
                    getActivity().finish();
                }
            }
        };

        MyDialog.drawable = getContext().getDrawable(R.drawable.dialog_confirm_driver);

        // 显示提示框
        MyDialog.showInfoDialog(getActivity(), "您是否已将乘客送到终点？", "取消", onCancelClickListener, "确认", onConfirmClickListener);
    }

    private void initView() {
        driverArrivedBtn = rootView.findViewById(R.id.driver_arrived_btn);
    }
}